// pages/ExchangeCoin/index.js
var network = require("../../utils/NetWorkUtils.js")
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commodityLength: 0,
    commodityList: [],
    advertisementBanner:'',
    jump_object:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestButtomBanner()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestCommodity();
  },

  //请求头部banner图片
  requestButtomBanner: function () {
    var that = this;
    var requestData = { position: 2 }
    network.requestHttp("m=api&c=Index&a=banner", requestData, function (res) {
      console.log(res)
      that.setData({
        advertisementBanner: res.data.data.pic,
        jump_object: res.data.data
      })
    })
  },

  //请求首页商品兑换接口
  requestCommodity: function () {
    var that = this;
    var dataparam = { type: 3 }
    network.requestHttp("m=api&c=Index&a=change", dataparam, function (data) {
      console.log(data)
      that.setData({
        commodityLength: data.data.data.prize2.length,
        commodityList: data.data.data.prize2,
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    // wx.showShareMenu({
    //   withShareTicket: true
    // })
    //如果是通过转发答题按钮分享的需要加一次答题机会
    console.log(options.from)
    if (options.from == 'button') {
      return {
        title: '我正在答题赚钱，免费兑换商品，快来和我一起抢！',
        path: 'pages/index/index?openid=' + app.globalData.openId,
        imageUrl: '../../images/share_pic.jpg',
      }
    } else {
      return {
        title: '我正在答题赚钱，免费兑换商品，快来和我一起抢！',
        path: 'pages/index/index',
        imageUrl: '../../images/share_pic.jpg',
      }
    }
  },
  //邀请免费兑换
  invite_exchange: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/InviteDetail/inviteDetail?id=' + e.currentTarget.dataset.id,
    })
  },
  //底部banner跳转
  banner_jump() {
    var dataParam = {
      id: 2
    }
    network.requestHttp("m=api&c=Index&a=clicked", dataParam, function (res) {

    })
    var that = this;
    if (that.data.jump_object.jump == '2') {
      wx.navigateToMiniProgram({
        appId: that.data.jump_object.appid,
        path: that.data.jump_object.path
      })
    } else {
      wx.navigateTo({
        url: '../webView/indexWebView?bannerUrl=' + that.data.jump_object.jump_url
      })
    }
  },
})